﻿using CSharpApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpIM.Http
{
    public class QueryExpander
    {
        /// <summary>
        /// all key is lowercased
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static KvUnit Expand(string query)
        {
            KvUnit values = new KvUnit();
            if (!string.IsNullOrEmpty(query))
            {
                var queries = query.Substring(1).Split('&');
                foreach (var item in queries)
                {
                    var pair = item.Split(new char[] { '=' }, 2);
                    if (pair.Length == 1)
                    {
                        if (pair[0].Length > 0)
                            values.Set(pair[0].ToLower(), "");
                    }
                    else
                    {
                        if (pair[0].Length > 0)
                            values.Set(pair[0].ToLower(), pair[1]);
                    }
                }
            }
            return values;
        }
    }
}
